var Msdb = require("../../../db/Msdb.js");
const crypto = require('crypto');
var request = require("request");
const schedule = require('node-schedule');  
/**
 * 发送短信
 * @param {string} phone 手机号
 * @param {string} zh 账号
 * @param {string} mm 密码
 * @param {string} tznr 通知内容
 */
async function sendHttp(phone,tznr) {
    const url = 'http://sms.qxcioud.com:9090/sms/batch/v1';
    const appkey = 'qGBLQs';
    const appsecret = '3kUUpb';
    const timestamp = Date.now().toString();
    const msg = '【奇讯云】'+ tznr;
    const appcode = '1000';
    const extend = '123';
    console.log(msg,"发送短信");

    const map = {
        appkey,
        appcode: appcode, // 确保appcode已定义
        timestamp,
        phone,
        msg,
        sign: md5(appkey + appsecret + timestamp),
    };

    const json = JSON.stringify(map);

    return new Promise(
        (resolve)=>{
          request({
            url:url,
            method:"POST",
            headers:{
              "Content-type": "application/json; charset=utf-8"
            },
            body:json

          }, async function (error, response, body){
            if(!error && response.statusCode == 200){
              console.log(body)
              resolve(body)
            }else{
              console.log("err--",error)
              resolve(3)
            }
          })
      })
}

/**
 * 获取短信状态报告
 */
async function getSmsStatusReport() {
  const url = 'http://sms.qxcioud.com:9090/sms/report/v1';
  const appkey = 'qGBLQs';
  const appsecret = '3kUUpb';
  const timestamp = Date.now().toString();
  const appcode = '1000';

  const map = {
      appkey,
      appcode: appcode, // 确保appcode已定义
      timestamp,
      sign: md5(appkey + appsecret + timestamp),
      number:1000
  };
  console.log(map,"map");
  const json = JSON.stringify(map);

  return new Promise(
      (resolve)=>{
        request({
          url:url,
          method:"POST",
          headers:{
            "Content-type": "application/json; charset=utf-8"
          },
          body:json

        }, async function (error, response, body){
          if(!error && response.statusCode == 200){
            console.log(body)
            resolve(body)
          }else{
            console.log("err--",error)
            resolve(3)
          }
        })
    })
}

function md5(param) {
    return crypto.createHash('md5').update(param, 'utf8').digest('hex');
}

// let res = sendHttp("18779135145","测试");
// console.log(res,"res");
// let res2 = getSmsStatusReport();
// console.log(res2,"res2");

module.exports = {
    sendHttp,
    getSmsStatusReport
};